package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.resp.ReturnCodeEnum;
import com.atguigu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author bairimengchang
 * @version 1.0
 * @project cloud2024
 * @description 核心控制器（这里成为生产者（流水管理）8001，供消费者（订单管理）80使用）
 * @date 2024/9/11
 */
@RestController
@Tag(name = "支付微服务模块",description = "支付CRUD")
@Slf4j
public class PayController{

    @Resource
    private PayService payService;

    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay){
        log.info("新增支付流水，参数为：{}", pay);
        int i = payService.add(pay);
        return ResultData.success("成功插入记录，返回值："+i);
    }

    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除",description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        log.info("删除支付流水，参数为：{}", id);
        int i = payService.delete(id);
        return ResultData.success(i);
    }

    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改",description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO){
        log.info("修改支付流水，参数为：{}", payDTO);
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int i = payService.update(pay);
        return ResultData.success("成功修改记录，返回值："+i);
    }
    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    public ResultData<Pay> getById(@PathVariable("id") Integer id){
        log.info("按照ID查询支付流水，参数为：{}", id);

        log.info("模拟服务超时");
        // 服务提供方故意作超时控制
        try {
            TimeUnit.SECONDS.sleep(62);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        Pay pay = payService.getById(id);
        return ResultData.success(pay);
    }

    @GetMapping(value = "/pay/getAll")
    @Operation(summary = "查询所有流水",description = "查询所有支付流水方法")
    public ResultData<List<Pay>> getAll(){
        log.info("查询所有支付流水");
        List<Pay> payList = payService.getAll();
        return ResultData.success(payList);
    }

    @GetMapping(value = "/pay/error")
    @Operation(summary = "异常测试",description = "异常测试方法")
    public ResultData<Integer> getPayError() {
        log.info("异常测试");
        Integer i = 200;
        try
        {
            System.out.println("--------come here");
            int data = 10/0;
        }catch (Exception e){
            log.error("异常信息：{}",e.getMessage());
            return ResultData.fail(ReturnCodeEnum.RC500.getCode(),e.getMessage());
        }
        return ResultData.success(i);
    }

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/get/info")
    private String getInfoByConsul(@Value("${atguigu.info}") String atguiguInfo) {
        return "atguiguInfo: "+atguiguInfo+"\t"+"port: "+port;
    }

//    @GetMapping(value = "/pay/get/info2")
//    private String getInfoByConsul2(@Value("${spring.datasource.type}") String type) {
//        // 实测这里能拿到配置在consul的值，并且数据库信息能正常访问
//        return "database name: "+type;
//    }
}
